package com.shanzmoo.h5.web;

import com.shanzmoo.h5.domain.query.ProductSpuCategoryQuery;
import com.shanzmoo.h5.domain.query.ProductSpuNameQuery;
import com.shanzmoo.h5.domain.vo.MallCategoryVO;
import com.shanzmoo.h5.domain.vo.IndexCategoryVo;
import com.shanzmoo.h5.domain.vo.ProductSpuBriefVo;
import com.shanzmoo.h5.domain.vo.ProductSpuInfoVo;
import com.shanzmoo.h5.service.IProductRedisService;
import com.shanzmoo.base.bean.PageRespBase;
import com.shanzmoo.base.bean.SHttpResponse;
import com.shanzmoo.base.bean.SResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "商品服务-开放接口")
@RestController
@RequestMapping("open/product")
public class ProductOpenController {

    @Autowired
    private IProductRedisService productRedisService;

    @ApiOperation("首页banner图")
    @GetMapping("banner")
    public SHttpResponse<List<String>> getBannerList() {
        List<String> list = productRedisService.getBannerList();
        return SHttpResponse.ok(list);
    }

    @ApiOperation("首页一级分类")
    @GetMapping("index/category")
    public SHttpResponse<List<IndexCategoryVo>> getIndexCategoryList() {
        List<IndexCategoryVo> result = productRedisService.getIndexCategoryList();
        return SHttpResponse.ok(result);
    }

    @ApiOperation("推荐商品")
    @GetMapping("recommend/list")
    public SHttpResponse<PageRespBase<ProductSpuBriefVo>> getRecommendList(
            @ApiParam(value = "当前页") @RequestParam(value = "pageNum") Integer pageNum,
            @ApiParam(value = "每页条数") @RequestParam(value = "pageSize") Integer pageSize) {
        PageRespBase<ProductSpuBriefVo> result = productRedisService.getRecommendList(pageNum, pageSize);
        return SHttpResponse.ok(result);
    }

    @ApiOperation("商品详情")
    @GetMapping("info")
    public SHttpResponse<ProductSpuInfoVo> getInfo(@ApiParam(value = "商品code") @RequestParam(value = "spu") String spu) {
        SResponse<ProductSpuInfoVo> result = productRedisService.getInfo(spu);
        return SHttpResponse.transform(result);
    }

    @ApiOperation("二、三级商品分类")
    @GetMapping("categoryList")
    public SHttpResponse<List<MallCategoryVO>> getCategoryList(
            @ApiParam(value = "一级分类id") @RequestParam(value = "categoryId") Integer categoryId) {
        SResponse<List<MallCategoryVO>> result = productRedisService.getCategoryList(categoryId);

        return SHttpResponse.transform(result);
    }


    @ApiOperation("三级分类商品列表")
    @PostMapping("queryByCategory")
    public SHttpResponse<PageRespBase<ProductSpuBriefVo>> queryProductByCategory(@RequestBody ProductSpuCategoryQuery queryDto) {
        PageRespBase<ProductSpuBriefVo> result = productRedisService.queryList(queryDto.convertToQuery());
        return SHttpResponse.ok(result);
    }

    @ApiOperation("商品搜索")
    @PostMapping("queryByName")
    public SHttpResponse<PageRespBase<ProductSpuBriefVo>> queryList(@RequestBody ProductSpuNameQuery queryDto) {
        PageRespBase<ProductSpuBriefVo> result = productRedisService.queryList(queryDto.convertToQuery());
        return SHttpResponse.ok(result);
    }
}
